const Koa = require('koa2'),
    cheerio = require('cheerio'),
    superagent = require('superagent'),
    fs = require('fs'),
    request = require('request'),
    app = new Koa()

const Info = {
    url: "https://www.meishichina.com/",
    nullPicPath: "https://img.alicdn.com/tfs/TB1JWN9qggP7K4jSZFqXXamhVXa-200-200.png"
}

let crawlList = [];

(function () {
    superagent.get(Info.url).end(async (err, res) => {
        let html = res.text,
            $ = cheerio.load(html, {
                decodeEntities: false
            })
        $(".big4_list > ul > li > a > i > img").each((index, item) => {
            let getInfo = {
                title: item.attribs.alt,
                imgPath: Object.values(item.attribs)[3].indexOf('https://') == '-1' ? `https:${Object.values(item.attribs)[3]}` : Object.values(item.attribs)[3]
                // imgPath: Object.values(item.attribs)[3].indexOf('https://') == '-1' ? Info.nullPicPath : Object.values(item.attribs)[3]
            }
            crawlList.push(getInfo)
        })
        await mkdirPath(__dirname + '/images')
        downLoad(crawlList)
        await isFileExisted(__dirname + '/foodList.txt')
        fs.writeFile('./foodList.txt', JSON.stringify(crawlList, null, '\t'), function (err) {
            if (err) {
                console.log('写入失败', err);
                return;
            } else {
                console.log('写入成功');
            }
        })
    })
})();

/**
 * @description 保存图片到本地
 * @param {Array} item 
 */
function downLoad(item) {
    console.log('downloading...')
    for (const key in item) {
        let filename = item[key].title,
            getPic = item[key].imgPath,
            writeStream = fs.createWriteStream(`./images/${filename}.jpg`),
            readStream = request(getPic)

        readStream.pipe(writeStream);
        readStream.on('end', function () {
            let result = getPic.indexOf('https://img.alicdn.com/tfs/') !== -1
            console.log(filename + `图片下载${result ? '失败' : '成功'}`);
        });
        readStream.on('error', function (err) {
            console.log(filename + "错误信息:" + err)
        })
        writeStream.on("finish", function () {
            writeStream.end();
        });
    }
}

/**
 * @description 判断文件是否存在
 * @param {String} path_way 文件路径
 * @returns 
 */
function isFileExisted(path_way) {
    return new Promise((resolve, reject) => {
        fs.access(path_way, (err) => {
            console.log(`${path_way} ${err ? '不存在' : '存在'}`);
            if (err) {
                try {
                    fs.appendFileSync(path_way, '', 'utf8');
                    console.log("txt文件不存在，已新创建");
                    resolve(true);
                } catch (error) {
                    console.log('txt文件不存在，重新创建失败！')
                    reject(false);
                }
            } else {
                resolve(true);
            }
        })
    })
};

/**
 * @description 判断文件夹是否存
 * @param {String} path_way 文件夹路径
 * @returns 文件夹路径地址
 */
function mkdirPath(path_way) {
    return new Promise((resolve, reject) => {
        fs.access(path_way, fs.constants.F_OK, (err) => {
            console.log(`${path_way} ${err ? '不存在' : '存在'}`);
            if (err) {
                fs.mkdirSync(path_way, { recursive: true }, (err) => {
                    if (err) {
                        console.log('images文件夹不存在，重新创建失败！')
                        reject(false);
                    }
                });
                console.log("images文件夹不存在，已新创建");
                resolve(true);
            } else {
                resolve(true);
            }
        });
    })
};


app.listen(7000, () => {
    console.log('loading...')
})